﻿<UserControl x:Class="DynamicModel.Wpf.Demo.Views.PersonDetailsView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <SolidColorBrush x:Key="DisabledForegroundBrush"
                         Color="Red" />
        <SolidColorBrush x:Key="DisabledBackgroundBrush"
                         Color="White" />
        <Style x:Name="test"
               TargetType="TextBox">
            <Setter Property="Background"
                    Value="White" />
            <Setter Property="BorderBrush"
                    Value="Black" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="TextBox">
                        <Border Name="Bd"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                Background="{TemplateBinding Background}"
                                SnapsToDevicePixels="true">
                            <ScrollViewer Name="PART_ContentHost"
                                          Background="{TemplateBinding Background}"
                                          SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled"
                                     Value="False">
                                <Setter Value="{StaticResource DisabledBackgroundBrush}"
                                        Property="Background" />
                                <Setter Value="{StaticResource DisabledForegroundBrush}"
                                        Property="Foreground" />
                                <!--<Setter TargetName="PART_ContentHost" Property="Background" Value="Blue"/>-->
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="textBoxInError"
               TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <Trigger Property="Validation.HasError"
                         Value="true">
                    <Setter Property="ToolTip"
                            Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                        Path=(Validation.Errors)[0].ErrorContent}" />
                </Trigger>
            </Style.Triggers>
        </Style>


        <Style x:Key="textBoxInError2"
               TargetType="{x:Type TextBox}">
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <DockPanel LastChildFill="True">
                            <TextBlock DockPanel.Dock="Right"
                                       Foreground="Orange"
                                       FontSize="12pt"
                                       Text="{Binding ElementName=MyAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
                            </TextBlock>
                            <Border BorderBrush="Green"
                                    BorderThickness="1">
                                <AdornedElementPlaceholder Name="MyAdorner" />
                            </Border>
                        </DockPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <ControlTemplate x:Key="validationTemplate">
            <DockPanel>
                <TextBlock Foreground="Red"
                           FontSize="20">!</TextBlock>
                <Border BorderBrush="Green"
                        BorderThickness="1">
                    <AdornedElementPlaceholder />
                </Border>

            </DockPanel>
        </ControlTemplate>
    </UserControl.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />
            <RowDefinition Height="30" />
            <RowDefinition Height="40" />
            <RowDefinition />
        </Grid.RowDefinitions>
        
        <Label Content="Id"
               Grid.Row="0"
               Grid.Column="0" />
        <TextBox Text="{Binding Model.Id,
            NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"
                 Validation.ErrorTemplate="{StaticResource validationTemplate}"
                 Style="{StaticResource textBoxInError}"
                 Grid.Row="0"
                 Grid.Column="1" />

        <Label Content="First Name"
               Grid.Row="1"
               Grid.Column="0" />
        <TextBox Text="{Binding Model.FName
            ,NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"
                 Validation.ErrorTemplate="{StaticResource validationTemplate}"
                 Style="{StaticResource textBoxInError}"
                 Grid.Row="1"
                 Grid.Column="1" />

        <Label Content="Last Name"
               Grid.Row="2"
               Grid.Column="0" />
        <TextBox Text="{Binding Model.LName, 
                        NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"
                 Validation.ErrorTemplate="{StaticResource validationTemplate}"
                 Style="{StaticResource textBoxInError}"                 
                 Grid.Row="2"
                 Grid.Column="1" />

        <Label Content="Active"
               Grid.Row="3"
               Grid.Column="0" />
        <CheckBox IsChecked="{Binding Model.Active,NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"
                 Grid.Row="3"
                 Grid.Column="1" />

        <Button Grid.Row="5"
                Grid.Column="0"
                Content="Cancel"  Command="{Binding CancelCommand}"/>
        <Button Grid.Row="5"
                Grid.Column="1"
                Content="Save"
                Command="{Binding SaveCommand}" CommandParameter="{Binding Path=Model }" />
    </Grid>
</UserControl>
